#pragma once
#include<pthread.h>

namespace LockModule
{
    class Mutex
    {
    public:
        Mutex(const Mutex&)=delete;//每一把锁都是唯一的，所以禁了
        const Mutex& operator=(const Mutex&)=delete;

        Mutex()
        {
            ::pthread_mutex_init(&_lock,nullptr);
        }
        ~Mutex()
        {
            ::pthread_mutex_destroy(&_lock);
        }
        void Lock()
        {
            ::pthread_mutex_lock(&_lock);
        }
        void Unlock()
        {
            ::pthread_mutex_unlock(&_lock);
            
        }

    private:
        pthread_mutex_t _lock;
    };

    class LockGuard
    {
    public:
        LockGuard( Mutex& mtx)
        :_mtx(mtx)
        {
            _mtx.Lock();
        }

        ~LockGuard()
        {
            _mtx.Unlock();
        }
    private:
        Mutex& _mtx;//使用引用确保是同一把锁

    };
}